package com.joy.eduservice.controller;


import com.joy.commonutils.R;
import com.joy.eduservice.client.VodClient;
import com.joy.eduservice.entity.EduVideo;
import com.joy.eduservice.service.EduVideoService;
import com.joy.servicebase.exceptionHandler.GuliException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-02
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    //删除小节
    // TODO 需要完善，删除小节时候，也要删除视频(已完善)
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id){
        //此时的id为小节id，需要根据小节id得到视频id

        //先根据小节id获得对象EduVideo
        EduVideo eduVideo = eduVideoService.getById(id);
        //再通过对象获取视频id
        String videoSourceId = eduVideo.getVideoSourceId();
        //判断小节里面是有有视频id
        if(!StringUtils.isEmpty(videoSourceId)){
            vodClient.removeAlyiVideo(videoSourceId);
        }else{
            throw new GuliException(20001,"此小结没有视频");
        }
        //删除小节
        eduVideoService.removeById(id);
        return R.ok();
    }

    //修改小节 TODO
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

}

